﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class LineFlights : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("IA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Session["FLT_DATE"] != null)
                    txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
                if (Session["ORI"] != null)
                    txtOriCity.Text = Session["ORI"].ToString();
                if (Session["DES"] != null)
                    txtDesCity.Text = Session["DES"].ToString();
            }
            lblError.Text = "";
            btnSetStatus.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnSetTailNo.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            Session["FLT_DATE"] = txtFltDate.Text;
            Session["ORI"] = txtOriCity.Text.ToUpper();
            Session["DES"] = txtDesCity.Text.ToUpper();
            try
            {
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        protected void btnRoute_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltRoute.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnSegments_Click(object sender, EventArgs e)
        {
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select a flight.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');

            //Response.Redirect("flt_segs.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnCabins_Click(object sender, EventArgs e)
        {
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select a flight.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');

            //Response.Redirect("flt_cabins.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnClasses_Click(object sender, EventArgs e)
        {
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select a flight.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');

            //Response.Redirect("flt_classes.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnSetStatus_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("IA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you wanted.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "bll_flt_set_status";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_date", strArray[1]);
                para = cmd.Parameters.AddWithValue("@status", lstStatus.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Updating failed!";
                else if (ret_code == 2)
                    lblError.Text = "Invalid status code!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSetTailNo_Click(object sender, EventArgs e)
        {
            //此应为离港功能？执飞飞机座位数对销售有作用，所以为座控职能！
            if (Session["ACL"].ToString().IndexOf("IA3") < 0)
            {
                lblError.Text = "unauthorized.aspx";
                return;
            }
            
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you wanted.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "bll_flt_set_aircraft";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_date", strArray[1]);
                para = cmd.Parameters.AddWithValue("@aircraft", lstTailNo.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@flt_type", lstFltTypes.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@surcharge_type", lstSurchargeTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //lstFltTypes.SelectedValue = "?";
                lstSurchargeTypes.SelectedValue = "?";
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done. Please check Cabin/Class and Departure Data.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Failed! Flt route not found!";
                else if (ret_code == 2)
                    lblError.Text = "Adding new cabin failed!";
                else if (ret_code == 3)
                    lblError.Text = "Updating tail number failed!";
                else if (ret_code == 4)
                    lblError.Text = "Please lock the flight before changing aircraft, or flight not found!";
                else if (ret_code == 5)
                    lblError.Text = "Failed! Check aircraft cabin data!";
                else if (ret_code == 6)
                    lblError.Text = "Please close check-in before changing aircraft, or flight not found!";
                else if (ret_code == 7)
                    lblError.Text = "Insert new seat map failed!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSetTypes_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("IA3") < 0)
            {
                lblError.Text = "unauthorized.aspx";
                return;
            }
            if (lstSurchargeTypes.SelectedValue == "?" /*|| lstFltTypes.SelectedValue == "?"*/)
            {
                lblError.Text = "Please select SurchargeType!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you wanted.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "bll_flt_set_surcharge_type";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_date", strArray[1]);
                //para = cmd.Parameters.AddWithValue("@flt_type", lstFltTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@surcharge_type", lstSurchargeTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //lstFltTypes.SelectedValue = "?";
                lstSurchargeTypes.SelectedValue = "?";
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Setting failed!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnControl_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltInventory.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1] + "&airline=" + strArray[2]);
        }
        protected void btnPaxList_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltNotPaidOrders.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnStandBy_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltCandidates.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnTickets_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltTickets.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnLog_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltLog.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnExpired_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltExpiredOrders.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnTransferOut_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("transfer_out.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnTransferIn_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("transfer_in.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }

        protected void btnSaleIndex_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltSaleIndex.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
        protected void btnCancelled_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("FltCancelledOrders.aspx?flt_no=" + strArray[0] + "&flt_date=" + strArray[1]);
        }
    }
}